/**
 * @BelongsProject: xx-club
 * @BelongsPackage: com.wx.subject.application.controller
 * @Author: wangx
 * @CreateTime: 2024-03-11  17:25
 * @Description: TODO
 * @Version: 1.0
 */
package com.wx.subject.application.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.subject.application.dto.SubjectLabelDTO;
import com.wx.subject.infra.basic.utils.MyUtils;
import com.wx.subject.common.entity.Result;
import com.wx.subject.infra.basic.entity.SubjectLabel;
import com.wx.subject.infra.basic.service.SubjectLabelService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
public class SubjectLabelController {

    @Resource
    private SubjectLabelService subjectLabelService;

    /**
     * 添加标签
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (subjectLabelDTO == null||subjectLabelDTO.getCategoryId()==null||subjectLabelDTO.getLabelName()==null){
            return Result.fail("参数不能为空");
        }
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setCategoryId(subjectLabelDTO.getCategoryId().toString());
        subjectLabel.setSortNum(subjectLabelDTO.getSortNum());
        subjectLabel.setLabelName(subjectLabelDTO.getLabelName());
        SubjectLabel insert = subjectLabelService.insert(subjectLabel);
        return Result.ok(insert);
    }

    /**
     * 更新标签
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (subjectLabelDTO == null||subjectLabelDTO.getId()==null||subjectLabelDTO.getLabelName()==null||subjectLabelDTO.getSortNum()==null){
            return Result.fail("参数不能为空");
        }
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setId(subjectLabelDTO.getId());
        subjectLabel.setSortNum(subjectLabelDTO.getSortNum());
        subjectLabel.setLabelName(subjectLabelDTO.getLabelName());
        SubjectLabel update = subjectLabelService.update(subjectLabel);
        return Result.ok(update);
    }
    /**
     *  删除标签
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if (subjectLabelDTO == null||subjectLabelDTO.getId()==null){
            return Result.fail("参数不能为空");
        }
        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setId(subjectLabelDTO.getId());
        return Result.ok(subjectLabelService.deleteById(subjectLabel.getId()));
    }
    /**
     * 根据分类查询标签
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
     MyUtils.getInstance().DtoToEntity(subjectLabelDTO, SubjectLabel.class);
        List<SubjectLabel> subjectLabels = subjectLabelService.list(new QueryWrapper<SubjectLabel>().eq("category_id", subjectLabelDTO.getCategoryId()));
        return  Result.ok(subjectLabels);
    }
}


